/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.app.utils;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.logging.Level;
import javax.ws.rs.core.HttpHeaders;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.app.dto.Filter;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author LamLe
 */
public class Utils {

    private static Logger logger = Logger.getLogger(Utils.class);

    public static boolean filterURL(String url) {
        boolean isFilter = true;
        for (String s : Filter.getFilter()) {
            if (url.contains(s)) {
                isFilter = false;
                break;
            }
        }
        if (!isFilter) {
            logger.info("Filter '" + isFilter + "' for URL : " + url);
        }
        return isFilter;
    }

    public static String getDomain(String url) {
        String domain = "";
        domain = url.substring(7);
        domain = domain.substring(0, domain.indexOf("/"));
        return domain;
    }

    public static void main(String a[]) {
        try {
            String s = FileUtils.readFileToString(new File("resources/khoi_phuc_giang_son.htm"));
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static String filterString(Document doc) {
        StringBuilder sb = new StringBuilder("");
        try {
            Elements links = doc.select("a");
            for (Element e : links) {
                String url = e.attr("href");
                url = URLDecoder.decode(url, "UTF-8");
                if (url.contains("fshare.vn/file")) {
                    sb.append(url).append("\n");
                }
            }
        } catch (Exception e) {
            logger.error("filterString", e);
        }
        return sb.toString();
    }

    public static void initRequestHeader(Connection conn) throws Exception {
        try {
            conn.header(HttpHeaders.HOST, "www.google.com.vn")
                    .header(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0")
                    .header(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                    .header(HttpHeaders.ACCEPT_LANGUAGE, "en-US,en;q=0.5")
                    .header(HttpHeaders.ACCEPT_ENCODING, "gzip,deflate,sdch")
                    .timeout(10000);
        } catch (Exception e) {
            logger.error("Failed", e);
        }
    }

    public static String getDomainName(String url) throws URISyntaxException {
        URI uri = new URI(url);
        String domain = uri.getHost();
        return domain.startsWith("www.") ? domain.substring(4) : domain;
    }
}
